﻿using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Security;
using System.Security.Permissions;

namespace Spider.Data
{
     class CommonDbDataProviderFactory : DbProviderFactory
    {
        private const string ConncetionSettingName = "DefaultConnection";
        private static string connectionString =null; 
        private static DbProviderFactory provider = null;
        public CommonDbDataProviderFactory()
            : this(ConncetionSettingName)
        {
        }
        public CommonDbDataProviderFactory(string conncetionSettingName)
        {
            ConnectionStringSettings settings=ConfigurationManager.ConnectionStrings[conncetionSettingName];
            provider=DbProviderFactories.GetFactory(settings.ProviderName);
            connectionString = settings.ConnectionString;
        }
        public override DbConnection CreateConnection()
        {
            return CreateConnection(connectionString);
        }
        public DbConnection CreateConnection(string connectionString)
        {
            DbConnection con = provider.CreateConnection();
            con.ConnectionString = connectionString;
            return con;
        }
        public override DbCommand CreateCommand()
        {
            return provider.CreateCommand();
        }
        public DbCommand CreateCommand(string commandText)
        {
            DbCommand cmd = CreateCommand();
            cmd.CommandText = commandText;
            return cmd;
        }
        public DbCommand CreateCommand(string commandText,DbConnection con)
        {
            DbCommand cmd = CreateCommand();
            cmd.Connection = con;
            cmd.CommandText = commandText;
            return cmd;
        }
        public DbCommand CreateCommand(string commandText,CommandType commandType)
        {
            DbCommand cmd = CreateCommand(commandText);
            cmd.CommandType = commandType;
            return cmd;
        }
        public DbCommand CreateCommand(string commandText, CommandType commandType, DbConnection con)
        {
            DbCommand cmd = CreateCommand(commandText, commandType);
            cmd.Connection = con;
            return cmd;
        }
        public override DbParameter CreateParameter()
        {
            return provider.CreateParameter();
        }
        public DbParameter CreateParameter(string parameterName,object value,int size,DbType dbType,ParameterDirection parameterDirection)
        {
            DbParameter parameter=CreateParameter(parameterName,value,size,dbType);
            parameter.Direction = parameterDirection;
            return parameter;
        }
        public DbParameter CreateParameter(string parameterName, object value, int size, DbType dbType)
        {
            DbParameter parameter = CreateParameter(parameterName,value,dbType);
            parameter.Size = size;
            return parameter;
        }
        public DbParameter CreateParameter(string parameterName, object value, DbType dbType)
        {
            DbParameter parameter = CreateParameter(parameterName, value);
            parameter.DbType = dbType;
            return parameter;
        }
        public DbParameter CreateParameter(string parameterName, object value)
        {
            DbParameter parameter = CreateParameter(parameterName);
            parameter.Value = value;
            return parameter;
        }
        public DbParameter CreateParameter(string parameterName)
        {
            DbParameter parameter = CreateParameter();
            parameter.ParameterName = parameterName;
            return parameter;
        }
        public override DbDataAdapter CreateDataAdapter()
        {
            return provider.CreateDataAdapter();
        }

        public override bool CanCreateDataSourceEnumerator
        {
            get
            {
                return provider.CanCreateDataSourceEnumerator;
            }
        }

        public override DbCommandBuilder CreateCommandBuilder()
        {
            return provider.CreateCommandBuilder();
        }

        public override DbConnectionStringBuilder CreateConnectionStringBuilder()
        {
            return provider.CreateConnectionStringBuilder();
        }

        public override DbDataSourceEnumerator CreateDataSourceEnumerator()
        {
            return provider.CreateDataSourceEnumerator();
        }

        public override CodeAccessPermission CreatePermission(PermissionState state)
        {
            return provider.CreatePermission(state);
        }

        public override bool Equals(object obj)
        {
            return provider.Equals(obj);
        }

        public override int GetHashCode()
        {
            return provider.GetHashCode();
        }

        public override string ToString()
        {
            return provider.ToString();
        }
    }
}
